package com.yswy.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yswy.app.dto.AddUserLookPlayLogVo;
import com.yswy.app.service.SysConfigService;
import com.yswy.app.service.UserJlAdLogService;
import com.yswy.app.service.UserMoneyService;
import com.yswy.common.enums.CsjJlAdEnums;
import com.yswy.common.exception.LookJlAdException;
import com.yswy.common.exception.MoneyUnlockPlayException;
import com.yswy.domain.entity.UserMoney;
import com.yswy.domain.mapper.UserMoneyMapper;
import com.yswy.theater.entity.TheaterPlay;
import com.yswy.theater.entity.TheaterPlayGather;
import com.yswy.theater.service.TheaterPlayService;
import com.yswy.theater.service.TheaterPlayUnlockService;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;

import static com.yswy.domain.common.util.TokenUtils.getUserId;


@Slf4j
@Service
public class UserMoneyServiceImpl extends ServiceImpl<UserMoneyMapper, UserMoney>
        implements UserMoneyService {
    @Autowired
    private UserMoneyLogServiceImpl userMoneyLogService;
    @Autowired
    private SysConfigService configService;
    @Autowired
    private TheaterPlayUnlockService theaterPlayUnlockService ;
    @Autowired
    private TheaterPlayService theaterPlayService ;
    @Autowired
    private  UserJlAdLogService userJlAdLogService ;
    @Override
    public Integer addByPay(Long userId , Integer money) {
        UserMoney userMoney = getUserMoney(userId);
        userMoney.setMoneyDec(userMoney.getMoneyDec()+money);
        updateById(userMoney);
        userMoneyLogService.addUserMoneyLog(userId , 1 , 1 , money ,"购买");
        return userMoney.getMoneyDec();
    }


    @Override
    public UserMoney getUserMoney(Long userId) {
        UserMoney userMoney = getOne(new LambdaQueryWrapper<UserMoney>().eq(UserMoney::getUserId ,userId));
        if (userMoney == null){
            userMoney = UserMoney.builder().userId(userId).moneyDec(0).build();
            save(userMoney);
        }
        return userMoney;
    }

    @Override
    @Transactional
    public TheaterPlayGather usdByTheaterPlayUnlock(AddUserLookPlayLogVo addUserLookPlayLogVo, Long userId ) {

        Long playId  = addUserLookPlayLogVo.getPlayId();
        Integer episodes  = addUserLookPlayLogVo.getEpisodes();
        //解锁方式  0 广告解锁  1 金币解锁
        Integer unlockType  = addUserLookPlayLogVo.getUnlockType();
        String transId = addUserLookPlayLogVo.getTransId();

        TheaterPlayGather theaterPlayGather = theaterPlayUnlockService.TheaterPlayUnlockVerify(playId , episodes , userId);
        TheaterPlay theaterPlay = theaterPlayService.getById(playId);
        //解锁金币
        Integer unlockSpecies = theaterPlay.getUnlockSpecies();
        if (unlockType == 0){
            //广告解锁
            //验证是否看过广告
            if (!theaterPlay.isAdUnlock()){
                throw new IllegalArgumentException("该剧不支持广告解锁");
            }
            Boolean isLook = userJlAdLogService.usdUserJlAdLog(userId ,
                    transId, CsjJlAdEnums.WYJCPLAYLOOK.getMessage() );
            if (!isLook){
                throw new LookJlAdException("未观看广告");
            }
        }else {

            UserMoney userMoney = getUserMoney(userId);
            if (userMoney.getMoneyDec() < unlockSpecies){
                throw new MoneyUnlockPlayException("金币不足");
            }
            userMoney.setMoneyDec(userMoney.getMoneyDec()-unlockSpecies);
            updateById(userMoney);
            userMoneyLogService.addUserMoneyLog(userId , 2 , 2 ,
                    unlockSpecies,"购买《"+theaterPlay.getPlayName()+"》"+"第"+episodes+"集");
        }
        theaterPlayUnlockService.TheaterPlayUnlock(theaterPlayGather , userId);
        return theaterPlayGather;

    }
}
